<?php


namespace app\command;

use app\crm\model\CustomerAnalyseModel;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use Workerman\Crontab\Crontab;
use Workerman\Worker;

class Timer extends Command
{
    /**
     * @var int
     */
    protected $timer;
    /**
     * @var int|float
     */
    protected $interval = 2;
    protected function configure()
    {
        // 指令配置
        $this->setName('timer')
            ->addArgument('status', Argument::REQUIRED, 'start/stop/reload/status/connections')
            ->addOption('d', null, Option::VALUE_NONE, 'daemon（守护进程）方式启动')
            ->addOption('i', null, Option::VALUE_OPTIONAL, '多长时间执行一次')
            ->setDescription('开启/关闭/重启 定时任务');
    }
    protected function init(Input $input, Output $output)
    {
        global $argv;
        if ($input->hasOption('i'))
            $this->interval = floatval($input->getOption('i'));
        $argv[1] = $input->getArgument('status') ?: 'start';
        if ($input->hasOption('d')) {
            $argv[2] = '-d';
        } else {
            unset($argv[2]);
        }
    }
    protected function execute(Input $input, Output $output)
    {
        $this->init($input, $output);
        //创建定时器任务
        $task = new Worker();
        $task->count = 1;
        $task->name = "计划任务";
        $task->onWorkerStart = [$this, 'start'];
        $task->runAll();
    }
    public function stop()
    {
        //手动暂停定时器
        \Workerman\Lib\Timer::del($this->timer);
    }
    public function start()
    {
        new Crontab('0 10 2 * * *', function(){
//            echo date('Y-m-d')."\n";
//            $logFile = 'storage/log.txt';
//
//            $handle = fopen($logFile, 'ab');
//
//            fwrite($handle, date('Y-m-d H:i:s')."\n");
//
//            fclose($handle);
            $start = strtotime(date('Y-m-d',strtotime('-2 month')));
            $end = strtotime(date('Y-m-d'));
            while($start<$end)
            {
                $this->setCustomerAnalyse(date('Y-m-d',$start));
                $start+=24*60*60;
            }
        });
    }
    public function setCustomerAnalyse($date)
    {
        $now = date('Y-m-d',strtotime('-1 day'));
        print_r($date.PHP_EOL);
        $bh = [1,2,3,4,5,6,7,8,9];
        $dd = [10];
        $xm = [11,12];

        $start_time = strtotime($date);
        $end_time = strtotime($date)+24*60*60-1;
        $data = [];
        $baseWhere = [];
        $whereOr = [];
        array_push($whereOr,['a.first_time','between',[$start_time,$end_time]]);
        array_push($whereOr,['a.second_time','between',[$start_time,$end_time]]);
        if(!empty($request['shopIds']))
        {
            array_push($baseWhere,['a.shop_id','in',$request['shopIds']]);
        }
        $where = array_merge($baseWhere,[['b.customer_setting_id_type','in',$bh]]);
        $res = Db::name('customer')->alias('a')
            ->join('customer_scar b','a.id = b.customer_id','left')
            ->join('shop c','a.shop_id = c.shop_id','left')
            ->field('count(a.id) as num,a.shop_id,c.shop_name,c.market_name,c.market_id')
            ->where($where)
            ->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })
            ->group('a.shop_id')
            ->order('c.market_id')
            ->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['date'] = $start_time;
            $data[$value['shop_id']]['shop_id'] = $value['shop_id'];
            $data[$value['shop_id']]['bh_store'] = $value['num'];
        }
        $where = array_merge($baseWhere,[['b.customer_setting_id_type','in',$dd]]);
        $res = Db::name('customer')->alias('a')
            ->join('customer_scar b','a.id = b.customer_id','left')
            ->join('shop c','a.shop_id = c.shop_id','left')
            ->field('count(a.id) as num,a.shop_id,c.shop_name,c.market_name,c.market_id')
            ->where($where)
            ->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })
            ->group('a.shop_id')
            ->order('c.market_id')
            ->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['date'] = $start_time;
            $data[$value['shop_id']]['shop_id'] = $value['shop_id'];
            $data[$value['shop_id']]['dd_store'] = $value['num'];
        }
        $where = array_merge($baseWhere,[['b.customer_setting_id_type','in',$xm]]);
        $res = Db::name('customer')->alias('a')
            ->join('customer_scar b','a.id = b.customer_id','left')
            ->join('shop c','a.shop_id = c.shop_id','left')
            ->field('count(a.id) as num,a.shop_id,c.shop_name,c.market_name,c.market_id')
            ->where($where)
            ->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })
            ->group('a.shop_id')
            ->order('c.market_id')
            ->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['date'] = $start_time;
            $data[$value['shop_id']]['shop_id'] = $value['shop_id'];
            $data[$value['shop_id']]['xm_store'] = $value['num'];
        }


        //体验数
        $where = [];
        array_push($where,['b.goods_id','=',168]);
        array_push($where,['c.customer_setting_id_type','in',$bh]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('customer_scar c','a.id = c.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(b.customer_id)) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['bh_receive']=$value['num'];
        }

        $where = [];
        array_push($where,['b.goods_id','=',686]);
        array_push($where,['c.customer_setting_id_type','in',$dd]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('customer_scar c','a.id = c.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(b.customer_id)) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['dd_receive']=$value['num'];
        }

        $where = [];
        array_push($where,['b.goods_id','in',[150,165]]);
        array_push($where,['c.customer_setting_id_type','in',$xm]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('customer_scar c','a.id = c.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(b.customer_id)) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['xm_receive']=$value['num'];
        }

        //体验顾客成交数
        $where = [];
        array_push($where,['b.goods_id','=',168]);
        array_push($where,['a.cj_money','>=',500]);
        array_push($where,['d.customer_setting_id_type','in',$bh]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            // ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(a.id)) as num')->group('a.shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['bh_receive_deal']=$value['num'];
        }

        $where = [];
        array_push($where,['b.goods_id','=',686]);
        array_push($where,['a.cj_money','>=',500]);
        array_push($where,['d.customer_setting_id_type','in',$dd]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            // ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(a.id)) as num')->group('a.shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['dd_receive_deal']=$value['num'];
        }

        $where = [];
        array_push($where,['b.goods_id','in',[150,165]]);
        array_push($where,['a.cj_money','>=',500]);
        array_push($where,['d.customer_setting_id_type','in',$xm]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            // ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(a.id)) as num')->group('a.shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['xm_receive_deal']=$value['num'];
        }

        //体验金额
        $where = [];
        array_push($where,['b.goods_id','=',168]);
        array_push($where,['d.customer_setting_id_type','in',$bh]);
        array_push($where,['c.business_type','=',1]);
        array_push($where,['c.status','=',2]);
        array_push($where,['c.extra_type','=',0]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,sum(c.pay_amount) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['bh_receive_money']=$value['num'];
        }
        $where = [];
        array_push($where,['b.goods_id','=',686]);
        array_push($where,['d.customer_setting_id_type','in',$dd]);
        array_push($where,['c.business_type','=',1]);
        array_push($where,['c.status','=',2]);
        array_push($where,['c.extra_type','=',1]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,sum(c.pay_amount) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['dd_receive_money']=$value['num'];
        }

        $where = [];
        array_push($where,['b.goods_id','in',[150,165]]);
        array_push($where,['d.customer_setting_id_type','in',$xm]);
        array_push($where,['c.business_type','=',2]);
        array_push($where,['c.status','=',2]);
        // array_push($where,['c.extra_type','=',0]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,sum(c.pay_amount) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['xm_receive_money']=$value['num'];
        }

        //直接进店人数
        $where = [];
        array_push($where,['b.goods_id','not in',[168,165,150,686]]);
        array_push($where,['a.cj_money','>=',500]);
        array_push($where,['d.customer_setting_id_type','in',$bh]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            // ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(a.id)) as num')->group('a.shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['bh_direct']=$value['num'];
        }
        $where = [];
        array_push($where,['b.goods_id','not in',[168,165,150,686]]);
        array_push($where,['a.cj_money','>=',500]);
        array_push($where,['d.customer_setting_id_type','in',$dd]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            // ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(a.id)) as num')->group('a.shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['dd_direct']=$value['num'];
        }
        $where = [];
        array_push($where,['b.goods_id','not in',[168,165,150,686]]);
        array_push($where,['a.cj_money','>=',500]);
        array_push($where,['d.customer_setting_id_type','in',$xm]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            // ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,count(distinct(a.id)) as num')->group('a.shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['xm_direct']=$value['num'];
        }
        //直接进店金额
        $where = [];
        array_push($where,['b.goods_id','not in',[168,165,150,686]]);
        array_push($where,['d.customer_setting_id_type','in',$bh]);
        array_push($where,['c.business_type','=',1]);
        array_push($where,['c.status','=',2]);
        array_push($where,['c.extra_type','=',0]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,sum(c.pay_amount) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['bh_direct_money']=$value['num'];
        }
        $where = [];
        array_push($where,['b.goods_id','not in',[168,165,150,686]]);
        array_push($where,['d.customer_setting_id_type','in',$dd]);
        array_push($where,['c.business_type','=',1]);
        array_push($where,['c.status','=',2]);
        array_push($where,['c.extra_type','=',1]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,sum(c.pay_amount) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['dd_direct_money']=$value['num'];
        }

        $where = [];
        array_push($where,['b.goods_id','not in',[168,165,150,686]]);
        array_push($where,['d.customer_setting_id_type','in',$xm]);
        array_push($where,['c.business_type','=',2]);
        array_push($where,['c.status','=',2]);
        array_push($where,['c.extra_type','=',0]);
        if(!empty($request['shopIds']))
        {
            array_push($where,['a.shop_id','in',$request['shopIds']]);
        }
        $res = Db::name('customer')->alias('a')
            ->join('service_log b','a.id = b.customer_id','left')
            ->join('shop_refund_order c','a.id = c.uid')
            ->join('customer_scar d','a.id = d.customer_id','left')
            ->where($where)->where(function($query)use($whereOr){
                $query->whereOr($whereOr);
            })->field('a.shop_id,sum(c.pay_amount) as num')->group('shop_id')->select()->toArray();
        foreach($res as $value)
        {
            $data[$value['shop_id']]['xm_direct_money']=$value['num'];
        }
        foreach($data as $key => $value)
        {
            if(empty($value['bh_store']))
            {
                $data[$key]['bh_store']=0;
            }
            if(empty($value['dd_store']))
            {
                $data[$key]['dd_store']=0;
            }
            if(empty($value['xm_store']))
            {
                $data[$key]['xm_store']=0;
            }
            if(empty($value['bh_receive']))
            {
                $data[$key]['bh_receive']=0;
            }
            if(empty($value['dd_receive']))
            {
                $data[$key]['dd_receive']=0;
            }
            if(empty($value['xm_receive']))
            {
                $data[$key]['xm_receive']=0;
            }
            if(empty($value['bh_receive_deal']))
            {
                $data[$key]['bh_receive_deal']=0;
            }
            if(empty($value['dd_receive_deal']))
            {
                $data[$key]['dd_receive_deal']=0;
            }
            if(empty($value['xm_receive_deal']))
            {
                $data[$key]['xm_receive_deal']=0;
            }
            if(empty($value['bh_receive_money']))
            {
                $data[$key]['bh_receive_money']=0;
            }
            if(empty($value['dd_receive_money']))
            {
                $data[$key]['dd_receive_money']=0;
            }
            if(empty($value['xm_receive_money']))
            {
                $data[$key]['xm_receive_money']=0;
            }
            if(empty($value['bh_direct']))
            {
                $data[$key]['bh_direct']=0;
            }
            if(empty($value['dd_direct']))
            {
                $data[$key]['dd_direct']=0;
            }
            if(empty($value['xm_direct']))
            {
                $data[$key]['xm_direct']=0;
            }
            if(empty($value['bh_direct_money']))
            {
                $data[$key]['bh_direct_money']=0;
            }
            if(empty($value['dd_direct_money']))
            {
                $data[$key]['dd_direct_money']=0;
            }
            if(empty($value['xm_direct_money']))
            {
                $data[$key]['xm_direct_money']=0;
            }
            $data[$key]['store_sum']=$data[$key]['bh_store']+$data[$key]['dd_store']+$data[$key]['xm_store'];
            $data[$key]['receive_sum']=$data[$key]['bh_receive']+$data[$key]['dd_receive']+$data[$key]['xm_receive'];
            $data[$key]['receive_deal_sum']=$data[$key]['bh_receive_deal']+$data[$key]['dd_receive_deal']+$data[$key]['xm_receive_deal'];
            $data[$key]['receive_money_sum']=$data[$key]['bh_receive_money']+$data[$key]['dd_receive_money']+$data[$key]['xm_receive_money'];
            $data[$key]['direct_sum']=$data[$key]['bh_direct']+$data[$key]['dd_direct']+$data[$key]['xm_direct'];
            $data[$key]['direct_money_sum']=$data[$key]['bh_direct_money']+$data[$key]['dd_direct_money']+$data[$key]['xm_direct_money'];
            krsort($data[$key]);
        }
        if($now == $date)
        {
            $data = array_merge($data,[]);
            Db::name('shop_customer_analyse')->insertAll($data);
        }else{
            $result = CustomerAnalyseModel::where('date','=',strtotime($date))->column('id','shop_id');
            foreach ($data as $key => $value)
            {
                if(!empty($result[$key]))
                    $data[$key]['id']=$result[$key];
            }
            $data = array_merge($data,[]);
            $model = new CustomerAnalyseModel;
            $model->saveAll($data);

        }
        return true;
    }
}
